using UnityEngine;

public class DeathBringer : Enemy
{
    public DeathBringerGroundState Ground { get; private set; }
    public DeathBringerIdleState Idle { get; private set; }
    public DeathBringerBattleState Battle { get; private set; }
    public DeathBringerAttackState Attack { get; private set; }
    public DeathBringerDeathState Death { get; private set; }
    public DeathBringerTeleportState Teleport { get; private set; }
    public DeathBringerSpellCastState SpellCast { get; private set; }
    public bool bossFightBegun;
    [Header("Teleport details")]
    private BoxCollider2D arena;
    [SerializeField] private Vector2 surroundingCheckSize;
    public float defaultChanceToTeleport = 25;
    [HideInInspector] public float chanceToTeleport;
    [Header("Spell cast details")]
    private GameObject spellPrefab;
    public int AmountOfSpells;
    public float SpellCooldown;
    public float lastTimeCast;
    [SerializeField] private float spellStateCooldown;
    [SerializeField] private Vector2 spellOffset = new Vector2(3f, 3f);

    protected override void Awake()
    {
        base.Awake();
        chanceToTeleport = defaultChanceToTeleport;
        spellPrefab = Resources.Load<GameObject>("Prefabs/DeathBringerSpellController");
        Ground = new(this, "idle");
        Idle = new(this, "idle");
        Battle = new(this, "move");
        Attack = new(this, "attack");
        Death = new(this, "idle");
        Teleport = new(this, "teleport");
        SpellCast = new(this, "spellCast");
        StateMachine.Initialize(Idle);
    }

    void Start()
    {
        arena = GameObject.Find("ArenaCollider").GetComponent<BoxCollider2D>();
    }

    public void CastSpell()
    {
        float xOffset;
        if (Player.Rb.velocity.x != 0)
        {
            xOffset = Player.FacingDirection * spellOffset.x;
        }
        else
        {
            xOffset = 0;
        }
        Vector3 position = new Vector3(Player.transform.position.x + xOffset, Player.transform.position.y + spellOffset.y);
        GameObject obj = Instantiate(spellPrefab, position, Quaternion.identity);
        obj.GetComponent<DeathBringerSpellController>().Setup(Stats);
    }

    public void FindPosition()
    {
        float x = Random.Range(arena.bounds.min.x + 3, arena.bounds.max.x + 3);
        float y = Random.Range(arena.bounds.min.y + 3, arena.bounds.max.y + 3);
        transform.position = new Vector3(x, y);
        transform.position = new Vector3(transform.position.x, transform.position.y - GroundBlow().distance + arena.size.y / 2);
        if (!GroundBlow() || SomethingIsAround())
        {
            Debug.Log("Looking for new position");
            FindPosition();
        }
    }

    private RaycastHit2D GroundBlow() => Physics2D.Raycast(transform.position, Vector2.down, 100, groundLayer);

    private bool SomethingIsAround() => Physics2D.BoxCast(transform.position, surroundingCheckSize, 0, Vector2.zero, 0, groundLayer);

    public override bool CanBeStunned()
    {
        if (canBeStunned)
        {
            // StateMachine.ChangeState(StunnedState);
            return true;
        }
        return false;
    }

    public override void Die()
    {
        StateMachine.ChangeTo(Death);
    }

    public bool CanTeleport()
    {
        if (Random.Range(0, 100) <= chanceToTeleport)
        {
            chanceToTeleport = defaultChanceToTeleport;
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool CanDoSpellCast()
    {
        if (Time.time >= lastTimeCast + spellStateCooldown)
        {
            return true;
        }
        return false;
    }

#if UNITY_EDITOR
    protected override void OnDrawGizmos()
    {
        base.OnDrawGizmos();
        Gizmos.DrawLine(transform.position, new Vector3(transform.position.x, transform.position.y - GroundBlow().distance));
        Gizmos.DrawWireCube(transform.position, surroundingCheckSize);
    }
#endif
}
